package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.crm.entity.GwfaT;
import cn.e4j.modular.crm.entity.GwfalT;
import cn.e4j.modular.crm.mapper.GwfaTMapper;
import cn.e4j.modular.crm.service.GwfaTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuw
 * @since 2019-10-25
 */
@Service
public class GwfaTServiceImpl extends ServiceImpl<GwfaTMapper, GwfaT> implements GwfaTService {

    @Override
    @Transactional
    public List<Map<String, Object>> getGwfalist(Page<Object> page, Dto dto) {
        return this.baseMapper.getList(page,dto);
    }

    @Override
    @Transactional
    public void add(GwfaT gwfaT, GwfalT gwfalT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwfaT.setGWFAENT(shiroUser.getDBC());

        gwfalT.setGWFALENT(shiroUser.getDBC());
        gwfalT.setGWFAL001(gwfaT.getGWFA001());
        gwfalT.setGWFAL002(LangUtils.getLang(shiroUser.getLANG()));

        gwfaT.insert();
        gwfalT.insert();
    }

    @Override
    @Transactional
    public boolean existedTheParams(GwfaT gwfaT) {
        QueryWrapper<GwfaT> gwfaTQueryWrapper = new QueryWrapper<>();
        gwfaTQueryWrapper.eq("GWFAENT", gwfaT.getGWFAENT());
        gwfaTQueryWrapper.eq("GWFA001", gwfaT.getGWFA001());
        return this.baseMapper.selectList(gwfaTQueryWrapper).size() > 0;
    }

    @Override
    @Transactional
    public void edit(GwfaT gwfaT, GwfalT gwfalT) {
        ShiroUser shiroUser = ShiroKit.getUser();

        UpdateWrapper<GwfaT> gwfaTUpdateWrapper = new UpdateWrapper<>();
        gwfaTUpdateWrapper.eq("GWFAENT",gwfaT.getGWFAENT());
        gwfaTUpdateWrapper.eq("GWFA001",gwfaT.getGWFA001());

        UpdateWrapper<GwfalT> gwfalTUpdateWrapper = new UpdateWrapper<>();
        gwfalTUpdateWrapper.eq("GWFALENT",gwfaT.getGWFAENT());
        gwfalTUpdateWrapper.eq("GWFAL001",gwfaT.getGWFA001());
        gwfalTUpdateWrapper.eq("GWFAL002",LangUtils.getLang(shiroUser.getLANG()));

        gwfaT.update(gwfaTUpdateWrapper);
        gwfalT.update(gwfalTUpdateWrapper);
    }

    @Override
    @Transactional
    public void delete(GwfaT gwfaT, GwfalT gwfalT) {
        UpdateWrapper<GwfaT> gwfaTUpdateWrapper = new UpdateWrapper<>();
        gwfaTUpdateWrapper.eq("GWFAENT",gwfaT.getGWFAENT());
        gwfaTUpdateWrapper.eq("GWFA001",gwfaT.getGWFA001());

        UpdateWrapper<GwfalT> gwfalTUpdateWrapper = new UpdateWrapper<>();
        gwfalTUpdateWrapper.eq("GWFALENT",gwfaT.getGWFAENT());
        gwfalTUpdateWrapper.eq("GWFAL001",gwfaT.getGWFA001());

        gwfaT.delete(gwfaTUpdateWrapper);
        gwfalT.delete(gwfalTUpdateWrapper);
    }

    @Override
    @Transactional
    public void deleteBatch(List<GwfaT> gwfaTList) {
        for(GwfaT gwfaT : gwfaTList){
            UpdateWrapper<GwfaT> gwfaTUpdateWrapper = new UpdateWrapper<>();
            gwfaTUpdateWrapper.eq("GWFAENT",gwfaT.getGWFAENT());
            gwfaTUpdateWrapper.eq("GWFA001",gwfaT.getGWFA001());

            GwfalT gwfalT = new GwfalT();
            UpdateWrapper<GwfalT> gwfalTUpdateWrapper = new UpdateWrapper<>();
            gwfalTUpdateWrapper.eq("GWFALENT",gwfaT.getGWFAENT());
            gwfalTUpdateWrapper.eq("GWFAL001",gwfaT.getGWFA001());

            gwfaT.delete(gwfaTUpdateWrapper);
            gwfalT.delete(gwfalTUpdateWrapper);
        }
    }
}
